<template>
	<section>
		<!--工具条
		v-model="keyword" 接受到的数据
		-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true">
				<el-form-item>
					<el-input v-model="keyword" placeholder="关键字查询"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click ="keywordQuery">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click ="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="shop" highlight-current-row v-loading="listLoading"
              @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="60">
			</el-table-column>
			<el-table-column type="index" width="70">
			</el-table-column>
			<el-table-column prop="name" label="字典明细名称" width="150" sortable>
			</el-table-column>

      <el-table-column prop="type.name" label="字典明细类型" width="150" sortable>
      </el-table-column>

			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>

		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination  background layout="prev, pager, next"
              @current-change="handleCurrentChange"
             :total="totals"
             :current-page="currentPage"
             :page-size="pageSize"
             style="float:right;">
			</el-pagination>
		</el-col>

		<!--添加或修改的对话框-->
		<el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
			<el-form :model="saveForm" label-width="150px" :rules="saveFormRules" ref="saveForm">
				<el-form-item label="字典明细名称" prop="name">
					<el-input v-model="saveForm.name" ></el-input>
				</el-form-item>
        <el-form-item label="字典明细类型"prop="types_id">
          <el-select v-model="saveForm.types_id" placeholder="请选择" clearable>
            <el-option v-for="item in managers"
                       :label="item.name"
                       :value="item.id">
              <span style="float: left">{{ item.name }}</span>
            </el-option>
          </el-select>
        </el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="saveLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
  export default {
		data() {
			return {
        shop: [],//显示分页列表数据
        totals: 0,//总数量
        currentPage:1,//当前页
        pageSize:4,//每页显示的数量
        listLoading: false,//表格上的加载框

        //多条数据选中
        sels: [],//列表选中列

        //高级查询的数据
        keyword:"",

        managers:[],

        //添加或修改数据
        saveFormVisible : false,//编辑界面是否显示
        saveLoading: false,//加载框
        title:'',//对话框标题
        saveFormRules: {//验证规则
					names: [
						{ required: true, message: '请输入字典明细名称', trigger: 'blur' }
					],
          name: [
            { required: true, message: '请选择字典明细类型', trigger: 'blur' }
          ]
				},

				//编辑界面数据
        saveForm: {
					id: null,
					name: '',
          types_id:'',
          names: '',
          type:null,


				},
			}
		},
		methods: {
		  //1.分页查询
      getShop() {
        let para = {
          //分页参数
          currentPage: this.currentPage,
          pageSize: this.pageSize,
          //高级查询的关键字
          keyword:this.keyword
        };
        //页面一加载就显示加载框
        this.listLoading = true;
        this.$http.post("/dictionarydetail",para).then(res=> {
          //给totals赋值总数量 计算分页栏
          this.totals = res.data.totals;
          //赋值分页数据 显示当前页的分页数据
          this.shop = res.data.data;
          //隐藏加载框
          this.listLoading = false;

        }).catch(res=>{
          this.$message.error('系统繁忙，请稍后再试！！！！');
        });
      },


      //2.点击某一页触发方法
      handleCurrentChange(val) {
        this.currentPage = val;
        this.getShop();
      },


      //3.关键字查询
      keywordQuery(){
        //关键字从第一行开始查询
         this.currentPage = 1,
         this.getShop();
      },


      //4.单行删除
      handleDel: function (index, row) {
        this.$confirm('确认删除该记录吗?', '温馨提示', {
          type: 'warning'
        }).then(() => {
          //显示加载框
          this.listLoading = true;
          //获取id
          var id = row.id;
          this.$http.delete("/dictionarydetail/"+id).then(res => {
            if(res.data.success){//删除成功

              let totalPage = this.totals%this.pageSize==0?this.totals/this.pageSize
                  :Math.ceil(this.totals/this.pageSize);
              if(this.currentPage > 1 && this.currentPage==totalPage && (this.totals-1)%this.pageSize==0){
                this.currentPage = this.currentPage - 1;
              }

              this.$message.success('删除成功');
              this.getShop();
            }else{
              this.$message.error('系统繁忙，请稍后重试500');
            }
            this.listLoading = false;
          }).catch(res => {
            this.$message.error('系统繁忙，请稍后重试400，404');
          })
        }).catch(() => {
        });
      },


      //5.选项改变的时候触发
      selsChange: function (sels) {
        this.sels = sels;
      },


      //6.批量删除
      batchRemove: function () {
        var ids = this.sels.map(item => item.id)
        this.$confirm('确认删除选中记录吗？', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
         /* let para = { ids: ids };*/
         this.$http.patch("/dictionarydetail",ids).then(res =>{
           if(res.data.success){//删除成功
             this.$message.success('批量删除成功');
             this.getShop();
           }else{
             this.$message.error('系统繁忙，请稍后重试500');
           }
           this.listLoading = false;
         }).catch(res => {
           this.$message.error('系统繁忙，请稍后重试400，404');
         }).catch(() => {
         })
         })
      },


			//7.点击修改弹出对话框
			handleEdit: function (index, row) {
        this.title = "修 改";
				this.saveFormVisible = true;
				this.saveForm = Object.assign({}, row);
				//显示店长名单
        this.getManagers()
			},


			//8.点击添加弹出对话框
			handleAdd: function () {
        this.title = "添 加";
				this.saveFormVisible = true;
				this.getManagers();
				this.saveForm = {
          name: '',
          types_id:'',
          id:'',
          names:'',

				};
			},


			//9.提交修改或者添加的数据
      editSubmit: function () {
        this.$refs.saveForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.saveLoading = true;
              let para = Object.assign({}, this.saveForm);
              this.$http.put("/dictionarydetail",para).then(res =>{
                this.saveFormVisible = false;
                this.saveLoading = false;
                if(res.data.success){
                   this.$message.success("操作成功");
                   this.getShop();
                }else {
                  this.$message.error("操作失败【500】")
                }
              }).catch(res =>{
                this.$message.error("操作失败【400】,【404】")
              })

            })
          }
        });
      },

      //查找所有部门员工的数量
      getManagers(){
        this.$http.get("/dictionarytype").then(res => {
          this.managers = res.data;

        })
      },

	},
		mounted() {
			this.getShop();
		}
	}

</script>

<style scoped>

</style>